//取cookies
function getCookie(name) {
    let arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    //arr  user=name,,name,
    if (arr != null) return unescape(arr[2]);
    return null;
}

//删除cookie
function delCookie(name) {
    let cookie = getCookie(name);
    const exp = new Date();
    exp.setTime(exp.getTime() - 1);
    if (cookie != null) {
        document.cookie = name + "=" + cookie + ";expires=" + exp.toUTCString() + ";path=/";
    }
    window.location.href = 'index';
}

//添加购物车
function addCartPro(id, count) {
    $.ajax({
        type: "GET",
        url: "cart/addPro",
        data: {
            proId: id,
            'count': count
        },
        success: function (result) {
            if (result.status === 1) {
                layer.msg('添加成功', {time: 1500, icon: 6});
            } else {
                window.location.href = 'index';
            }
        }
    })
}

//测试用户名
function testUsername() {
    const username = $('#username').val();
    let msg = $('#nameMsg');
    if (username.length < 2 || username.length >= 10) {
        msg.text('长度在2到10之间');
        return false;
    }
    if (username.toString().includes(' ')) {
        msg.text('不能包含空格');
        return false;
    }
    return true;
}

//测试问题答案
function testAnswer() {
    const answer = $('#answer').val();
    const msg = $('#answerMsg');
    msg.text('请牢记您的答案');
    if (answer.length === 0) {
        msg.text('不能为空');
        return false;
    }
    return true;
}

//测试密码
function testPsw() {
    const psw = $('#password').val();
    const msg = $('#passwordMsg');
    msg.text('6到16个字符');
    if (psw.length < 6 || psw.length > 16) {
        msg.text('检查您输入的长度 6~16之间');
        return false;
    }
    return true;
}

//测试确认密码
function testRePass() {
    const psw = $('#password').val();
    const rePass = $('#rePass').val();
    const msg = $('#rePassMsg');
    msg.text('');
    if (psw !== rePass) {
        msg.text('两次输入不一样！！！');
        return false;
    }
    return true;
}